<template>
    <div>
        <el-upload
            class="upload-demo"
            action=""
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList"
            :http-request="uploadSectionFile"
            :on-remove="handleRemove"
            accept=".pdf,.PDF">
            <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
    </div>
</template>

<script>
    import { upLoadPdf } from '../api/upPdfApi'
    export default {
        name: "uploadPdf",
        data() {
            return {
            }
        },
        props: {
            fileList: {
                type: Array
            }
        },
        methods: {
            async uploadSectionFile(params) {
                const file = params.file
                console.log(file.type)
                if (!file.type.endsWith('/pdf')) {
                    this.$message.warning('请上传正确的pdf格式')
                    this.fileList = []
                    return false
                }
                const form = new FormData()
                form.append("file", file)
                const res = await upLoadPdf(form)
                if (res.code === 0) {
                    this.$message.success('上传成功')
                    console.log(res)
                    this.$emit('chooseFile', res.data.filename)
                }
            },
            handleExceed(files, fileList) {
                // this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                this.$message.warning(`当前限制选择 1 个文件`)
            },
            handleRemove(file) {
                console.log(file)
                this.$emit('chooseFile', false)
            }
        }
    }
</script>

<style scoped lang="scss">

</style>
